package md.lanjing.com.module_wallet.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;


import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_wallet.R;
import md.lanjing.com.module_wallet.R2;
import md.lanjing.com.module_wallet.bean.ChargeDrawRecordResponce;
import md.lanjing.com.module_wallet.contact.ChargeDrawRecordContact;
import md.lanjing.com.module_wallet.ui.adapter.ChargeDrawRecordAdapter;
import retrofit2.Response;

/**
 * 提充记录
 */
public class ChargeDrawRecordFragment extends MvpFragment<ChargeDrawRecordContact.ChargeDrawRecordPresent> implements ChargeDrawRecordContact.IChargeDrawRecordView {
    private static final String TYPE = "type";
    @BindView(R2.id.recy_record)
    RecyclerView recyRecord;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    List<ChargeDrawRecordResponce.DataBean> mBeanList;
    ChargeDrawRecordAdapter mAdapter;
    private int pageNo = 1;

    int type;
    public static ChargeDrawRecordFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(TYPE, type);
        ChargeDrawRecordFragment fragment = new ChargeDrawRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        type = getArguments().getInt(TYPE);
        mBeanList = new ArrayList<>();
        mAdapter = new ChargeDrawRecordAdapter(R.layout.recy_item_charge_draw_record, mBeanList);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        recyRecord.setLayoutManager(manager);
        recyRecord.setAdapter(mAdapter);

        mPresent.queryRecordList(context, pageNo, type);
        refresh.setOnRefreshListener(refreshLayout -> {
            pageNo = 1;
            mPresent.queryRecordList(context, pageNo, type);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNo++;
            mPresent.queryRecordList(context, pageNo, type);
        });
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_charge_draw_record;
    }

    @Override
    public void getRecordListData(Response<ChargeDrawRecordResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<ChargeDrawRecordResponce.DataBean> data = response.body().getData();
            if (pageNo == 1) {
                mBeanList.clear();
            }
            mBeanList.addAll(data);
            mAdapter.notifyDataSetChanged();
            if (data!=null&&data.size()==10){
                refresh.setEnableLoadMore(true);
            }else {
                refresh.setEnableLoadMore(false);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected ChargeDrawRecordContact.ChargeDrawRecordPresent createPresent() {
        return new ChargeDrawRecordContact.ChargeDrawRecordPresent();
    }
}
